package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.hdw.Item;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 商品切面类
 *
 * @Date:Created in 2018/7/10 16:10
 * @Author:
 * @Modified By: zjn 2018/7/10 16:10
 */
@Component
@Aspect
public class ItemServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemService.find(..))")
    private void find() {}
    /**
    * 查询更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  16:11
    */
    @Around("find()")
    public Object find(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM, id);

        Object result = memCachedClient.get(key);
        if(result == null) {
            result = joinPoint.proceed();
            if(result != null) {
                memCachedClient.set(key, result);
            }
        }

        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemService.update(..))")
    private void update() {}
    /**
    * 更新删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  16:11
    */
    @Around("update()")
    public Object update(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Item item = (Item) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM, item.getId());
        memCachedClient.delete(key);
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemService.updateOrderNum(..))")
    private void updateOrderNum() {}
    /**
    * 更新
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  16:18
    */ 
    @Around("updateOrderNum()")
    public Object updateOrderNum(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM, id);
        memCachedClient.delete(key);
        return result;
    }

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.hdw.ItemService.delete(..))")
    private void delete() {}
    /**
    * 删除
    * 
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/10  16:49
    */ 
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_ITEM, id);
        memCachedClient.delete(key);
        return result;
    }
}
